/*
日期格式化
*/
import { ref, computed } from 'vue';

export const useDataFormatter = (initialValue = []) => {
	const data = ref(initialValue);

	const formattedData = computed(() => {
		if (!data.value || data.value.length === 0) return [];
		return data.value.map(item => ({
			...item,
			formattedDate: item.created_at?.split(" ")[0] || ""
		}));
	});

	return {
		data,
		formattedData,
		loadData: async (fetchFn) => {
			try {
				const response = await fetchFn();
				data.value = response?.data || [];
			} catch (error) {
				console.error("加载数据失败:", error);
			}
		}
	};
}